﻿Imports System.Collections.ObjectModel
Imports System.IO
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.Command
Imports FL_8x0.Common.SystemData
Imports FL_8x0.Common.Database
Imports FL_8x0.Common.mdlDevice
Imports FL_8x0.Common.mdlLPUAddress
Imports FL_8x0.FL_830.Model

Namespace FL_8x0.FL_800.ViewModel

    Public Class SystemViewModel
        Inherits ViewModelBase

        Private _reason As String              '修改原因
        Private _enableSave As Boolean         '保存有效

        Private _applyCommand As RelayCommand   '点击“确认”触发命令
        Private _cancelCommand As RelayCommand  '点击“取消”触发命令


        Private _portNameList As ObservableCollection(Of String)        '串口号列表
        Private _pointList As ObservableCollection(Of String)           '小数点列表
        Private _ePointList As ObservableCollection(Of String)          '科学计数法小数点列表
        Private _baudRateList As ObservableCollection(Of String)        '波特率列表
        Private _endCharList As ObservableCollection(Of String)         '结尾字符列表


        Private _enableAutoSave As Boolean                              '自动保存曲线
        Private _systemModel As SystemModel

        Private _msVisibility As Visibility = Visibility.Collapsed


        Public Sub New()
            InitListSource()
            FillModel()
            EnableAutoSave = g_udtOtherSet.blnAutoSaveWave
            ApplyCommand = New RelayCommand(Sub() Apply())
            CancelCommand = New RelayCommand(Sub() Cancel())
            If Machine = Model_800 And Version = Version_800_127 Then
                MsVisibility = Visibility.Visible
            End If
        End Sub

        '=====================================================================
        '函 数 名: InitListSource
        '参    数: 
        '返 回 值:
        '功能描述: 初始化列表
        '=====================================================================
        Private Sub InitListSource()
            BaudRateList = New ObservableCollection(Of String) From {
                "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200"
                }
            PortNameList = New ObservableCollection(Of String)()
            For i = 1 To 32
                PortNameList.Add("COM" & i)
            Next

            PointList = New ObservableCollection(Of String) From {
                "0", "0.0", "0.00", "0.000", "0.0000", "0.00000"
                }
            EPointList = New ObservableCollection(Of String) From {
                "0E+0", "0.0E+0", "0.00E+0", "0.000E+0", "0.0000E+0", "0.00000E+0"
                }
            EndCharList = New ObservableCollection(Of String) From {
                "Cr", "Lf", "CrLf"
                }
        End Sub


        '=====================================================================
        '函 数 名: FillModel
        '参    数: 
        '返 回 值:
        '功能描述: 数据填充
        '=====================================================================
        Private Sub FillModel()
            Dim udtSystemModel As SystemModel = New SystemModel
            udtSystemModel.AutoSaveWave = g_udtOtherSet.blnAutoSaveWave
            udtSystemModel.SavePath = g_udtOtherSet.strAutoSaveWavePath
            udtSystemModel.GpAutoStretch = g_udtOtherSet.blnAutoAxisY
            udtSystemModel.GpMax = g_udtOtherSet.sngMaxAxisY
            udtSystemModel.GpMin = g_udtOtherSet.sngMinAxisY
            udtSystemModel.DpAutoStretch = g_udtOtherSet.blnAutoAxisY2
            udtSystemModel.DpMax = g_udtOtherSet.sngMaxAxisY2
            udtSystemModel.DpMin = g_udtOtherSet.sngMinAxisY2


            udtSystemModel.EnableReadPressure = User_ReadPublicValue("准备环节读取直压", False)
            udtSystemModel.ExitDetectionTip = User_ReadPublicValue("退出泄漏检测提示", False)
            udtSystemModel.SaveRecordTip = User_ReadPublicValue("导出报告提示", False)
            udtSystemModel.ExitAppTip = User_ReadPublicValue("程序退出提示", False)
            udtSystemModel.Username = User_ReadPublicValue("默认用户名", "")
            udtSystemModel.Password = User_ReadPublicValue("默认密码", "")
            udtSystemModel.AuditMode = User_ReadPublicValue("审计追踪模式", False)


            udtSystemModel.PressurePoint = g_udtOtherSet.uint直压_Point
            udtSystemModel.LeakPoint = g_udtOtherSet.uint漏率_Point

            udtSystemModel.EnableScanningGun = g_udtOtherSet.blnScanGuns
            udtSystemModel.AutoStart = g_udtOtherSet.blnAutoStart
            udtSystemModel.PortName = g_udtOtherSet.uintPortName
            udtSystemModel.BaudRate = g_udtOtherSet.uintBaudRate
            udtSystemModel.EndChar = g_udtOtherSet.uintEndChar

            udtSystemModel.WriteToMachine = g_udtOtherSet.blnWriteToMachine

            SystemModel = udtSystemModel
        End Sub


        '=====================================================================
        '函 数 名: Apply
        '参    数: 
        '返 回 值:
        '功能描述: 数据存库，更新设备参数
        '=====================================================================
        Private Sub Apply()

            If CheckInput() Then
                SystemModel.AutoSaveWave = EnableAutoSave
                g_udtOtherSet.blnAutoSaveWave = SystemModel.AutoSaveWave
                User_WritePublicValue("自动保存曲线", SystemModel.AutoSaveWave, Reason)

                If g_udtOtherSet.blnAutoSaveWave Then
                    g_udtOtherSet.strAutoSaveWavePath = SystemModel.SavePath
                    User_WritePublicValue("曲线自动保存目录", SystemModel.SavePath, Reason)
                End If


                g_udtOtherSet.blnAutoAxisY = SystemModel.GpAutoStretch
                User_WritePublicValue("曲线直压Y轴自动伸缩", SystemModel.GpAutoStretch, Reason)
                If g_udtOtherSet.blnAutoAxisY Then
                    g_udtOtherSet.sngMaxAxisY = SystemModel.GpMax
                    User_WritePublicValue("Y轴最大", SystemModel.GpMax, Reason)
                    g_udtOtherSet.sngMinAxisY = SystemModel.GpMin
                    User_WritePublicValue("Y轴最小", SystemModel.GpMin, Reason)
                End If


                g_udtOtherSet.blnAutoAxisY2 = SystemModel.DpAutoStretch
                User_WritePublicValue("曲线差压Y轴自动伸缩", SystemModel.DpAutoStretch, Reason)
                If g_udtOtherSet.blnAutoAxisY2 Then
                    g_udtOtherSet.sngMaxAxisY2 = SystemModel.DpMax
                    User_WritePublicValue("Y轴最大", SystemModel.DpMax, Reason)
                    g_udtOtherSet.sngMinAxisY2 = SystemModel.DpMin
                    User_WritePublicValue("Y轴最小", SystemModel.DpMin, Reason)
                End If


                g_udtOtherSet.uint直压_Point = SystemModel.PressurePoint
                User_WritePublicValue("压力小数点", SystemModel.PressurePoint, Reason)
                g_udtOtherSet.uint漏率_Point = SystemModel.PressurePoint
                User_WritePublicValue("漏率小数点", SystemModel.LeakPoint, Reason)

                g_udtOtherSet.blnEnableReadPresssure = SystemModel.EnableReadPressure
                User_WritePublicValue("准备环节读取直压", SystemModel.EnableReadPressure, Reason)

                User_WritePublicValue("退出泄漏检测提示", SystemModel.ExitDetectionTip, Reason)
                User_WritePublicValue("导出报告提示", SystemModel.SaveRecordTip, Reason)
                User_WritePublicValue("程序退出提示", SystemModel.ExitAppTip, Reason)

                g_udtOtherSet.strUserName = SystemModel.Username
                User_WritePublicValue("默认用户名", SystemModel.Username, Reason)
                g_udtOtherSet.strUserName = SystemModel.Password
                User_WritePublicValue("默认密码", SystemModel.Password, Reason)
                g_udtOtherSet.blnAuditMode = SystemModel.AuditMode
                User_WritePublicValue("审计追踪模式", SystemModel.AuditMode, Reason)

                g_udtOtherSet.blnScanGuns = SystemModel.EnableScanningGun
                User_WritePublicValue("扫描枪有效", SystemModel.EnableScanningGun, Reason)
                g_udtOtherSet.blnAutoStart = SystemModel.AutoStart
                User_WritePublicValue("扫描后自动启动", SystemModel.AutoStart, Reason)
                g_udtOtherSet.blnWriteToMachine = SystemModel.WriteToMachine
                User_WritePublicValue("条码写入仪器", SystemModel.WriteToMachine, Reason)

                g_udtOtherSet.uintBaudRate = SystemModel.BaudRate
                User_WritePublicValue("扫描枪_波特率", SystemModel.BaudRate, Reason)
                g_udtOtherSet.uintPortName = SystemModel.PortName
                User_WritePublicValue("扫描枪_串口号", SystemModel.PortName, Reason)
                g_udtOtherSet.uintEndChar = SystemModel.EndChar
                User_WritePublicValue("扫描枪_结尾", SystemModel.EndChar, Reason)

                Reason = ""
                MsgBox("参数保存成功", MsgBoxStyle.OkOnly, "提示")
            End If
        End Sub


        '=====================================================================
        '函 数 名: Cancel
        '参    数: 
        '返 回 值:
        '功能描述: 取消 回填数据
        '=====================================================================
        Private Sub Cancel()
            FillModel()
            EnableAutoSave = SystemModel.AutoSaveWave
        End Sub

        Private Function CheckInput() As Boolean

            If g_udtOtherSet.blnAuditMode And String.IsNullOrEmpty(Reason) Then
                log.WriteLogError("输入参数检查", "您当前处于审计模式，请填写修改原因！")
                Return False
            End If

            SystemModel.AutoSaveWave = EnableAutoSave
            If SystemModel.AutoSaveWave Then
                If SystemModel.SavePath = "" Then
                    log.WriteLogError("输入参数检查", "[曲线自动保存目录]不能为空！")
                    Return False
                End If

                If Directory.Exists(SystemModel.SavePath) = False Then
                    log.WriteLogError("输入参数检查", "[曲线自动保存目录]" & SystemModel.SavePath & "不存在！")
                    Return False
                End If
            End If

            If Not SystemModel.SavePath.EndsWith("\") Then
                SystemModel.SavePath &= "\"
            End If

            If Not SystemModel.GpAutoStretch Then
                If Not CheckTextNumber("直压Y轴最大", SystemModel.GpMax, 99999, -99999) Then
                    Return False
                End If

                If Not CheckTextNumber("直压Y轴最小", SystemModel.GpMin, 99999, -99999) Then
                    Return False
                End If

                Dim TOLERANCE As Single = 0.000001
                If Math.Abs(SystemModel.GpMax - 0) < TOLERANCE And Math.Abs(SystemModel.GpMin - 0) < TOLERANCE Then
                    If Math.Abs(SystemModel.GpMax - SystemModel.GpMin) < TOLERANCE Then
                        log.WriteLogError("输入参数检查", "[直压Y轴最大]必须大于[直压Y轴最小]！")
                    End If
                End If
            End If

            If Not SystemModel.DpAutoStretch Then
                If Not CheckTextNumber("差压Y轴最大", SystemModel.DpMax, 99999, -99999) Then
                    Return False
                End If

                If Not CheckTextNumber("差压Y轴最小", SystemModel.DpMin, 99999, -99999) Then
                    Return False
                End If

                Dim TOLERANCE As Single = 0.000001
                If Math.Abs(SystemModel.DpMax - 0) < TOLERANCE And Math.Abs(SystemModel.DpMin - 0) < TOLERANCE Then
                    If Math.Abs(SystemModel.DpMax - SystemModel.DpMin) < TOLERANCE Then
                        log.WriteLogError("输入参数检查", "[差压Y轴最大]必须大于[差压Y轴最小]！")
                    End If
                End If
            End If


            Return True
        End Function

        Public Property SystemModel As SystemModel
            Get
                Return _systemModel
            End Get
            Set
                _systemModel = Value
                RaisePropertyChanged()
            End Set
        End Property


        Public Property BaudRateList As ObservableCollection(Of String)
            Get
                Return _baudRateList
            End Get
            Set(value As ObservableCollection(Of String))
                _baudRateList = value
            End Set
        End Property

        Public Property PointList As ObservableCollection(Of String)
            Get
                Return _pointList
            End Get
            Set
                _pointList = Value
            End Set
        End Property

        Public Property EPointList As ObservableCollection(Of String)
            Get
                Return _ePointList
            End Get
            Set
                _ePointList = Value
            End Set
        End Property

        Public Property PortNameList As ObservableCollection(Of String)
            Get
                Return _portNameList
            End Get
            Set
                _portNameList = Value
            End Set
        End Property

        Public Property EndCharList As ObservableCollection(Of String)
            Get
                Return _endCharList
            End Get
            Set
                _endCharList = Value
            End Set
        End Property

        Public Property Reason As String
            Get
                Return _reason
            End Get
            Set
                _reason = Value
                EnableSave = Not String.IsNullOrWhiteSpace(_reason)
                RaisePropertyChanged()
            End Set
        End Property

        Public Property EnableSave As Boolean
            Get
                Return _enableSave
            End Get
            Set
                _enableSave = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property ApplyCommand As RelayCommand
            Get
                Return _applyCommand
            End Get
            Set
                _applyCommand = Value
            End Set
        End Property

        Public Property EnableAutoSave As Boolean
            Get
                Return _enableAutoSave
            End Get
            Set
                _enableAutoSave = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property CancelCommand As RelayCommand
            Get
                Return _cancelCommand
            End Get
            Set
                _cancelCommand = Value
            End Set
        End Property

        Public Property MsVisibility As Visibility
            Get
                Return _msVisibility
            End Get
            Set(value As Visibility)
                _msVisibility = value
            End Set
        End Property
    End Class
End Namespace